package com.hailiang.ing;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class LononServlet02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uname = req.getParameter("username");
        String pword = req.getParameter("password");
        String emal = req.getParameter("emal");
        System.out.println(uname);
        System.out.println(pword);
        System.out.println(emal);

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login?characterEncoding=utf8", "root", "root");
            PreparedStatement statement = con.prepareStatement("insert into userinfo VALUES(?,?,?,?)");

                if(uname!=null &&pword!=null &&emal!=null){
                    statement.setString(1,null);
                    statement.setString(2,uname);
                    statement.setString(3,pword);
                    statement.setString(4,emal);

                    int i = statement.executeUpdate();
                    System.out.println("注册成功");
                    resp.getWriter().print("ok");
                 }else{
                    resp.getWriter().print("no");
                }


        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
